#include "driver.h"
#include "port.h"
#include "driverjack.h"
#include "qkeymetro.h"
#include "buffer.h"

using namespace QKeyMetro;

Driver::Driver(void (*setBufferSize)(int), void (*setSampleRate)(int)):

        driverJack_ (new DriverJack("QKeyMetro")),
        inPort_(new Port(false, NC_STEREO, QString("Master_IN"))),
        outPort_(new Port(true, NC_STEREO, QString("Master_OUT")))
{

    driverJack_->init(setBufferSize, setSampleRate);
}


Driver::~Driver()
{
    driverJack_->stop();
}

bool Driver::start(){
    driverJack_->start();
    driverJack_->connectDefaultPorts();
    return true;
}

bool Driver::stop(){
    driverJack_->stop();
    return true;
}

QWaitCondition & Driver::getWaitCondition(){
    return driverJack_->getWaitCondition();
}

void Driver::connectPorts (Port & portOut, Port & portIn){
   driverJack_->registerPort( (DJ::Sample**) portOut.getBuffer(0)->getData(), true, portOut.getName() + QString("L"));
   driverJack_->registerPort((DJ::Sample**) portOut.getBuffer(1)->getData(), true, portOut.getName() + QString("R"));

   portIn.setBuffer(QSharedPointer<Buffer>(new Buffer()), 0);
   portIn.setBuffer(QSharedPointer<Buffer>(new Buffer()), 1);

   driverJack_->registerPort((DJ::Sample**) portIn.getBuffer(0)->getData(), false, portIn.getName() + QString("L"));
   driverJack_->registerPort((DJ::Sample**) portIn.getBuffer(1)->getData(), false, portIn.getName()+ QString("R"));
}

